package com.zqsign.common.core.logging;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @ClassName: ZqLoggerUtil 
 * @Description（类信息描述）: 日志工具类
 *
 * @Company: zqsign
 * @date 2017年10月26日 下午5:09:22
 */
public class ZqLoggerUtil {

	/**
	 * @Title: formatLogInfo
	 * @Description : 集成日志内容
	 * @date 2017年11月9日下午2:06:59
	 * @param args ：集成前数组
	 * @param newArray ：集成后数组
	 * @param project ：应用
	 * @param module ：模块
	 * @param function ：功能
	 */
	public static void formatLogInfo(Object[] args, Object[] newArray, String project, String module, String function) {
		newArray[0] = project;
		newArray[1] = module;
		newArray[2] = function;
		System.arraycopy(args, 0, newArray, 3, args.length);

	}

	/**
	 * @Title: getMethodName
	 * @Description : 获取调用方法名
	 * @date 2017年11月9日下午2:07:09
	 * @return
	 */
	public static String getMethodName() {

		StackTraceElement stack[] = (new Throwable()).getStackTrace();
		return stack[2].getMethodName();

	}

	/**
	 * @Title: getLineNumber
	 * @Description : 获取方法调用行数
	 * @date 2017年11月9日下午2:07:12
	 * @return
	 */
	public static int getLineNumber() {

		StackTraceElement stack[] = (new Throwable()).getStackTrace();
		return stack[2].getLineNumber();

	}

	/**
	 * @Title: getHostAddress
	 * @Description : 获取本机ip
	 * @date 2017年11月9日下午2:07:15
	 * @return
	 */
	public static String getHostAddress() {
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * @Title: getRemoteHost
	 * @Description : 获取请求ip
	 * @date 2017年11月9日下午2:07:19
	 * @param request
	 * @return
	 */
	public static String getRemoteHost(javax.servlet.http.HttpServletRequest request) {
		if (request == null) {
			return "0.0.0.0";
		}
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}
	/**
	 * @Title: getIpAddress
	 * @Description : 获取请求主机IP地址，如果通过代理进来，则透过防火墙获取真实IP地址;
	 * @date 2017年11月9日下午2:00:00
	 * @param request
	 * @return
	 */
	public final static String getIpAddress(javax.servlet.http.HttpServletRequest request) {
		// 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
		if (request == null) {
			return "0.0.0.0";
		}
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
		} else if (ip.length() > 15) {
			String[] ips = ip.split(",");
			for (int index = 0; index < ips.length; index++) {
				String strIp = (String) ips[index];
				if (!("unknown".equalsIgnoreCase(strIp))) {
					ip = strIp;
					break;
				}
			}
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}
}
